﻿using Infrastructure.Repository.Sys;
using Infrastructure;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreDomain.BusinessCore
{
    /// <summary>
    /// 角色权限核心
    /// </summary>
    public static class RoleCore
    {

        /// <summary>
        /// 角色仓储
        /// </summary>
        private static SysRoleRepository _sysRoleRepository = new();

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        private static List<SysRole> GetALLRoleByWhere(string corporationKey, string systemKey)
        {
            var roleList = _sysRoleRepository.GetRoleByCorporationKeyOrSystemKey(corporationKey, systemKey).Where(f=>f.IsOpen).ToList();
            return roleList;
        }

        /// <summary>
        /// 递归获取角色,组成树形结构
        /// </summary>
        /// <param name="menuList">角色数据</param>
        /// <returns>返回角色的树形结构</returns>
        public static List<SysRoleOutPut> GetRoleTreeList(List<SysRoleOutPut> sysRoleList)
        {
            List<SysRoleOutPut> list = [.. sysRoleList];
            List<SysRoleOutPut> sysRoleListDto = new();

            //递归所有父级角色
            foreach (var data in list.Where(f => f.Pid == 0))
            {
                var childrenList = GetChildrenRole(list, data.RoleId).ToList();
                data.Children = childrenList.Count == 0 ? null : childrenList;
                sysRoleListDto.Add(data);
            }
            return sysRoleListDto;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="moduleOutput">角色数据</param>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        private static List<SysRoleOutPut> GetChildrenRole(List<SysRoleOutPut> moduleOutput, int id)
        {
            List<SysRoleOutPut> sysRoleOutPut = [];
            //得到子角色
            var info = moduleOutput.Where(w => w.Pid == id).ToList();
            //循环
            foreach (var sysRoleInfo in info)
            {
                var childrenList = GetChildrenRole(moduleOutput, sysRoleInfo.RoleId);
                //把子角色放到Children集合里
                sysRoleInfo.Children = childrenList.Count == 0 ? null : childrenList;
                //添加父级角色
                sysRoleOutPut.Add(sysRoleInfo);
            }
            return sysRoleOutPut;
        }


        /// <summary>
        /// 根据角色id获取所有子集角色id
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns>返回所有下级角色</returns>
        public static List<int> GetChildrenRoleById(int id, string corporationKey, string systemKey)
        {
            List<int> idList = new();
            var roleList = GetALLRoleByWhere(corporationKey, systemKey);
            foreach (var item in roleList)
            {
                if (id == item.Pid)
                {
                    idList.Add(item.RoleId);
                    idList.AddRange(GetChildrenRoletId(item.RoleId, roleList));
                }
            }
            return idList;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="id">上级角色ID</param>
        /// <param name="sysRoleList">所有角色集合</param>
        /// <returns></returns>
        private static List<int> GetChildrenRoletId(int id, List<SysRole> sysRoleList)
        {
            List<int> idList = new();
            foreach (var item in sysRoleList)
            {
                if (id == item.Pid)
                {
                    idList.Add(item.RoleId);
                    GetChildrenRoletId(item.RoleId, sysRoleList);
                }
            }
            return idList;
        }
    }
}
